package com.sinoglobal.dumping.base;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.umeng.socialize.bean.CallbackConfig.ICallbackListener;
import com.umeng.socialize.bean.RequestType;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * @author lgy
 * @createdate 2015-1-29 下午3:41:25
 * @Description: 有分享界面需要继承的基类 需要关联
 * social_sdk_library_project这个library
 * 需要新调用shareInfo();
 * 然后调用share();
 */
public abstract class Dumpling_ShareAbstractActivity extends Dumpling_SinoBaseActivity {

    // 首先在您的Activity中添加如下成员变量
    protected final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.dumpling_share", RequestType.SOCIAL);
    protected ICallbackListener listener;
    protected UMImage umImage;
    protected QQShareContent qqShareContent;
    protected QZoneShareContent qZoneShareContent;
    protected WeiXinShareContent weixinContent;
    protected CircleShareContent circleMedia;
    protected SinaShareContent sinaContent;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        String qqAppID = Dumpling_SinoConstans.UM_ID_QQ;//1101480789
        String qqAppSecret =Dumpling_SinoConstans.UM_SECRET_QQ ;
        String wxAppID = Dumpling_SinoConstans.UM_ID_WX;
        String wxAppSecret = Dumpling_SinoConstans.UM_SECRET_WX;
        shareInfo(qqAppID, qqAppSecret, wxAppID, wxAppSecret);
    }

    /**
     * @param platform 分享平台 如果传null 使用友盟默认分享面板
     * @param shareUrl 分享链接
     * @param isIcon   资源文件id 如果没有传0 会分享picUrl
     *                 <p/>
     *                 (如果isIcon值为0,picUrl一定要有图片地址)
     * @param picUrl   网络图片
     * @author lgy
     * @createdate 2015-1-29 下午3:23:10
     * @Description: (用一句话描述该方法做什么) 分享面板打开和设置内容
     */


    public void setShare(String content, String title, SHARE_MEDIA platform, String shareUrl, final int isIcon, String picUrl) {

        if (isIcon == 0) {
            umImage = new UMImage(Dumpling_ShareAbstractActivity.this, picUrl);

        } else {
            umImage = new UMImage(Dumpling_ShareAbstractActivity.this, isIcon);
        }
        // 设置分享内容
        mController.setShareContent(content);
        mController.setShareImage(umImage);

        qZoneShareContent.setTargetUrl(shareUrl);
        qZoneShareContent.setTitle(title);
        qZoneShareContent.setShareContent(content);
        qZoneShareContent.setShareImage(umImage);
        mController.setShareMedia(qZoneShareContent);

        qqShareContent.setTargetUrl(shareUrl);
        qqShareContent.setTitle(title);
        qqShareContent.setShareContent(content);
        qqShareContent.setShareImage(umImage);
        mController.setShareMedia(qqShareContent);

        // 设置微信好友内容
        weixinContent.setTargetUrl(shareUrl);
        weixinContent.setTitle(title);
        weixinContent.setShareContent(content);
        weixinContent.setShareImage(umImage);
        mController.setShareMedia(weixinContent);

        // 设置微信朋友圈内容
        circleMedia.setTargetUrl(shareUrl);
        circleMedia.setShareContent(title);
        circleMedia.setTitle(content);
        circleMedia.setShareImage(umImage);
        mController.setShareMedia(circleMedia);

        sinaContent.setShareContent(title+shareUrl);
        sinaContent.setTitle(title+shareUrl);
        sinaContent.setShareImage(umImage);
        mController.setShareMedia(sinaContent);

        // 如果不是单个分享平台 使用友盟默认分享平台
        if (platform == null) {
            mController.registerListener(listener);
            try {
                mController.openShare(Dumpling_ShareAbstractActivity.this, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // 参数1为当前Activity对象， 参数2为要分享到的目标平台， 参数3为分享操作回调接口
            mController.postShare(Dumpling_ShareAbstractActivity.this, platform, new SnsPostListener() {

                @Override
                public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {
                    if (arg1 == StatusCode.ST_CODE_SUCCESSED) {
                        shareSuccess();
                    } else if (arg1 == 40000) {
                    } else {
                        shareFail();
                    }
                }

                @Override
                public void onStart() {
                    shareStart();
                }

            });
        }
        // }
    }

    private Toast toast = null;
    private long oneTime = 0;
    private long twoTime = 0;
    private String oldMsg;

    protected void showShortToast(String message) {
        if (toast == null) {
            toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (message.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast.show();
                }
            } else {
                oldMsg = message;
                toast.setText(message);
                toast.show();
            }
        }


    }

    /**
     * @author lgy
     * @createdate 2015-3-25 下午3:54:50
     * @Description: 设置初始化
     */
    protected void shareInfo(String qqAppID, String qqAppSecret, String wxAppID, String wxAppSecret) {
        mController.getConfig().setSsoHandler(new SinaSsoHandler()); // 如果新浪安装客户端,会走新浪客户端
        // 选择平台
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN, SHARE_MEDIA.SMS, SHARE_MEDIA.TENCENT);
        // 设置分享平台面板中显示的平台
        mController.getConfig().setPlatformOrder(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
                SHARE_MEDIA.QZONE);

        mController.getConfig().closeToast();// 关闭土司
        weixinContent = new WeiXinShareContent();
        circleMedia = new CircleShareContent();
        qZoneShareContent = new QZoneShareContent();
        qqShareContent = new QQShareContent();
        sinaContent = new SinaShareContent();

        // String qqAppID = getString(R.string.qqAppID);
        // String qqAppSecret = getString(R.string.qqAppSecret);
        // 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, qqAppID, qqAppSecret);
        qqSsoHandler.addToSocialSDK();

        // 参数1为当前Activity，参数2为开发者在QQ互联申请的APP ID，参数3为开发者在QQ互联申请的APP kEY.
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(this, qqAppID, qqAppSecret);
        qZoneSsoHandler.addToSocialSDK();

        // TODO 需要替换新申请的QQ互联分享key
        // String wxAppID = getString(R.string.wxAppID);
        // String wxAppSecret = getString(R.string.wxAppSecret);
        // 添加微信平台
        UMWXHandler wxHandler = new UMWXHandler(this, wxAppID, wxAppSecret);
        wxHandler.showCompressToast(false);
        wxHandler.addToSocialSDK();
        // 添加微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(this, wxAppID, wxAppSecret);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.showCompressToast(false);
        wxCircleHandler.addToSocialSDK();

        listener = new SnsPostListener() {

            @Override
            public void onComplete(SHARE_MEDIA arg0, int arg1, SocializeEntity arg2) {
                if (arg1 == StatusCode.ST_CODE_SUCCESSED) {
                    showShortToast("分享成功");
                    // 判断平台 请求完成任务接口

                } else if (arg1 == 40000) {
                } else {
                    showShortToast("分享失败");
                }
            }

            @Override
            public void onStart() {
                showShortToast("分享中");
            }

        };

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (listener != null) {
            mController.unregisterListener(listener);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** 使用SSO授权必须添加如下代码 */
        UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }



    /** 分享中 */
    protected void shareStart(){
        showShortToast("分享中");
    }

    /** 分享成功 */
    protected void shareSuccess(){
        showShortToast("分享成功");
    }

    /** 分享失败 */
    protected void shareFail(){
        showShortToast("分享失败");
    }
}
